import 'package:flutter/material.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/reg_exp_utils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';

import 'password_reset_state.dart';

class PasswordResetLogic extends GetxController {
  final PasswordResetState state = PasswordResetState();

  late TextEditingController userName, smsCode, password, password2;

  @override
  void onInit() {
    super.onInit();
    userName = TextEditingController();
    smsCode = TextEditingController();
    password = TextEditingController();
    password2 = TextEditingController();
  }

  @override
  void onReady() {
    super.onReady();
  }

  ///
  /// 获取短信验证码
  /// telephone   手机号码
  ///
  Future<void> getIdentifyingCode() async {
    if (userName.text.isEmpty) {
      toast.show("请输入手机号码");
      return;
    }

    Map<String, dynamic> param = {
      "oneParam": userName.text, // 手机号码
    };
    ResultData resultData = await Api.sendSignCodeByAliyun(param);
    toast.show(resultData.data["message"]);
  }

  ///
  /// 忘记密码
  /// loginName   账号
  /// smsCode     验证码
  /// password    新密码
  ///
  Future<void> retrieveLoginPassword() async {
    if (userName.text.isEmpty) {
      toast.show("请输入手机号码");
      return;
    }
    if (!RegExpUtils().isPassword1(password.text)) {
      toast.show("密码长度8~16，包含字母、数字和以下特殊字符（.@\$!%*#_~?&^）");
      return;
    }
    if (password2.text != password.text) {
      toast.show("密码不一致");
      return;
    }

    Map<String, dynamic> param = {
      "telephone": userName.text, // 用户手机号
      "smCode": smsCode.text,
      "newPwd": password.text,
      'repeatPwd': password.text, // 确认密码
      'tenantId': "HFXN", // 租户ID
      "type": "2", // 改密方式 1.原密码修改 2. 通过手机验证码修改
    };

    ResultData resultData = await Api.updateLoginPassword(param);
    toast.show(resultData.data["message"]);
    if (resultData.isSuccess) {
      Get.back();
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
